<?php

/**
 *  
 */
class AdminSiteUserServiceCategoryController extends AdminBaseController
{
    /**
     *  before ...
     */
    public function beforeAction($action)
    {
        parent::beforeAction($action);

        if( !$this->permissionManager->checkRole('work_manager') ) {
            Request::redirect('admin/');
        }

        $this->menus->setFocusByAction('adminSiteUserServiceCategory/list');
        return true;
    }

    /**
     *  index
     */
    public function actionIndex()
    {
        Request::redirect('list');
    }

    /**
     *  list
     */
    public function actionList( $page=1, $searchKey='' )
    {
        if( Request::isPost() ) {
            $posts = Request::getPostInfo();
            if( $posts['searchKey'] ) {
                $searchKey = $posts['searchKey'];
            }
        }

        $userServiceCategories = new UserServiceCategories();
        $myUserServiceCategories = $userServiceCategories->getUserServiceCategories( $searchKey, UserServiceCategory::STATUS_OPEN, $page );
        $rowCount = $userServiceCategories->getNumUserServiceCategories( $searchKey );

        $pageLimit = new PageLimitManager();
        $pageLimit->setBaseUrl( $this->createUrl('') );
        $pageLimit->setRowCount( $rowCount );
        $pageLimit->setPageShowCount( BaseMapper::DEFAULT_ROW_COUNT );
        $pageLimit->setPage( $page );
        $pageLimit->setSearchKey( $searchKey );

        $this->render('list',array(
            'searchKey' => $searchKey,
            'page' => $page,
            'userServiceCategories' => $myUserServiceCategories, 
            'pageLimit' => $pageLimit 
        ));
    }

    /**
     *  edit
     */
    public function actionEdit( $id )
    {
        $id = (int) $id;
        $userServiceCategories = new UserServiceCategories();
        $userServiceCategory = $userServiceCategories->getUserServiceCategory($id);
        if(!$userServiceCategory) {
            Request::redirect('list');
        }

        // update
        if( Request::isPost() ) {
            $posts = Request::getPostInfo();
            $userServiceCategory->setName                 ( $posts['name']                 );
            $userServiceCategory->setDescription          ( $posts['description']          );
            $userServiceCategory->setStatus               ( $posts['status']               );
            $userServiceCategory->setOrderedPages         ( $posts['orderedPages']         );

            $messages = $userServiceCategory->validate();
            $userServiceCategory->filter();
            if( $messages ) {
                SessionManager::setErrorMessages('更新失敗');
                $data['fieldsMessages'] = $messages;
                $data['userServiceCategory'] = $userServiceCategory;
                $this->render('edit', $data );
                return;
            }
            $userServiceCategories = new UserServiceCategories();
            $userServiceCategories->updateUserServiceCategory($userServiceCategory);
            SessionManager::setSuccessMessages('更新成功');

        }

        // edit
        $data['userServiceCategory'] = $userServiceCategory;
        $this->render('edit',$data);
    }

}
